<?php
namespace Service\Controller;

use Common\Controller\AdminbaseController;
use Label\Model\CategoryModel;

class AdminindexController extends AdminbaseController{

    protected $sp_model;
    protected $page_size;

    function _initialize() {
        parent::_initialize();
        $this->page_size    = 20;
        $this->sp_model     = D("Service/ServiceProvider");
    }

    // 服务商列表
    public function index() {
        $this->_lists(array("del_time" => 0));
        $this->display();
    }

    public function changeSp_sort() {
        $id = I('request.id');
        if ($id && $info = M('service_provider')->where(['id' => $id])->find()) {
            $sort = I('request.sort');
            if ($sort > 0 && $sort != $info['sort']) {
                M('service_provider')->where(['id' => $id])->save(['sort' => $sort]);
                $this->success("排序更新成功");
            }
        }
        $this->error('数据错误');
    }

    /**
     * desc     编辑服务商
     */
    public function edit_sp()
    {
        $id         = I("get.id", 0, 'intval');
        $org_info   = M('service_provider')
            ->alias('s')
            ->where(['id' => $id])
            ->field('s.*, (select group_concat(id, "|", areaname) from rx_area where find_in_set(id, s.city_ids)) as city_names')
            ->find();

        if (IS_POST) {
            $_POST['sp_info']['categorys']  = implode(',', $_POST['cate']);
            $sp_info                        = I('post.sp_info');
            $sp_info['service_desc']        = htmlspecialchars_decode($sp_info['service_desc']);
            $sp_info['benifit']             = htmlspecialchars_decode($sp_info['benifit']);
            $sp_info['condition']           = htmlspecialchars_decode($sp_info['condition']);
            $sp_info['modify_time']         = time();

            if ($id) {
                M('service_provider')->where(['id' => $id])->save($sp_info);
                // 处理失效的图片
                $logo = json_decode($org_info['logo'], true);
                unlink_useless_img($logo['logo'], $sp_info['logo']);
                $this->success("修改成功！");
            } else {
                unset($sp_info['id']);
                M('service_provider')->add($sp_info);
                $this->success("添加成功！");
            }
        } else {
            if ($org_info) {
                if ($delete = I("get.delete", 0, 'intval')) {
                    M('service_provider')->where(['id' => $id])->save(['del_time' => time()]);
                    $this->success("删除成功");
                }
                // 信息处理
                $city_names = array_filter(explode(',', $org_info['city_names']));
                unset($org_info['city_names']);
                foreach ($city_names as $v) {
                    list($city_id, $city_name) = explode('|', $v);
                    $org_info['city_names'][$city_id] = $city_name;
                }
                $org_info['categorys']  = array_filter(explode(',', $org_info['categorys']));
                $this->assign('sp_info', $org_info);
            }

            //服务商分类
            $categorys = M('category')->where(['type' => CategoryModel::CATEGORY_TYPE_SERVICE, 'del_time' => 0])->select();
            $this->assign('categorys', $categorys);

            // 城市列表
            $this->assign('area', json_encode(getArea(2)));

            $this->display();
        }
    }

    // 融资列表
    public function sp_financing_list()
    {
        $id = I('get.id');
        $lists = M('sp_financing')->where(['sp_id' => $id])->select();
        $this->assign('lists', $lists);
        $this->assign('sp_id', $id);
        $this->display();
    }

    /**
     * desc     服务商融资信息
     */
    public function editsp_financing()
    {
        $id         = I('get.id');
        $sp_id      = I('get.sp_id');
        $org_info   = M('sp_financing')->where(['id' => $id])->find();
        if (IS_POST) {
            $info = I('post.info');
            if (!isset($info['time']) || empty($info['time'])) {
                unset($info['time']);
            }
            if ($id) {
                // 更新
                M('sp_financing')->where(['id' => $id])->save($info);
                $this->success("修改成功");
            } else {
                // 新建
                unset($info['id']);
                M('sp_financing')->add($info);
                $this->success("添加成功");
            }
        } else {
            $del = I('get.del');
            if ($del) {
                if ($org_info) {
                    M('sp_financing')->where(['id' => $id])->delete();
                    $this->success("删除成功");
                } else {
                    $this->error("请求数据有误！");
                }
            }
            if ($org_info) {
                $this->assign('info', $org_info);
            }
            $this->assign('sp_id', $sp_id);
            $this->display();
        }
    }


    // 团队成员列表
    public function sp_member_list()
    {
        $id = I('get.id');
        $lists = M('sp_member')->where(['sp_id' => $id])->select();
        $this->assign('lists', $lists);
        $this->assign('sp_id', $id);
        $this->display();
    }
    /**
     * desc     服务商团队成员信息
     */
    public function editsp_member()
    {
        $id         = I('get.id');
        $sp_id      = I('get.sp_id');
        $org_info   = M('sp_member')->where(['id' => $id])->find();
        if (IS_POST) {
            $info = I('post.info');
            if ($id) {
                // 更新
                M('sp_member')->where(['id' => $id])->save($info);
                $this->success("修改成功");
            } else {
                // 新建
                unset($info['id']);
                M('sp_member')->add($info);
                $this->success("添加成功");
            }
        } else {
            $del = I('get.del');
            if ($del) {
                if ($org_info) {
                    M('sp_member')->where(['id' => $id])->delete();
                    $this->success("删除成功");
                } else {
                    $this->error("请求数据有误！");
                }
            }
            if ($org_info) {
                $this->assign('info', $org_info);
            }
            $this->assign('sp_id', $sp_id);
            $this->display();
        }
    }

    // 项目列表
    public function sp_project_list()
    {
        $id = I('get.id');
        $lists = M('sp_project')->where(['sp_id' => $id])->select();
        $this->assign('lists', $lists);
        $this->assign('sp_id', $id);
        $this->display();
    }
    /**
     * desc     服务商项目信息
     */
    public function editsp_project()
    {
        $id         = I('get.id');
        $sp_id      = I('get.sp_id');
        $org_info   = M('sp_project')->where(['id' => $id])->find();
        if (IS_POST) {
            $info               = I('post.info');
            $info['imgs']       = implode(',', $_POST['imgs']);
            $info['city_id']    = $_POST['city_id'];
            if ($id) {
                // 更新
                M('sp_project')->where(['id' => $id])->save($info);
                $this->success("修改成功");
            } else {
                // 新建
                unset($info['id']);
                M('sp_project')->add($info);
                $this->success("添加成功");
            }
        } else {
            $del = I('get.del');
            if ($del) {
                if ($org_info) {
                    M('sp_project')->where(['id' => $id])->delete();
                    $this->success("删除成功");
                } else {
                    $this->error("请求数据有误！");
                }
            }
            if ($org_info) {
                $org_info['imgs'] = array_filter(explode(',', $org_info['imgs']));
                $this->assign('info', $org_info);
                if ($org_info['city_id'] > 0) {
                    $path = getAreaParentPath($org_info['city_id'], getArea(2));
                    $path = $path ? $path : '';
                    $this->assign('path', json_encode($path));
                }
            }
            // 城市列表
            $this->assign('area', json_encode(getArea(2)));
            $this->assign('sp_id', $sp_id);
            $this->display();
        }
    }


    // 项目列表
    public function sp_link_list()
    {
        $id = I('get.id');
        $lists = M('sp_link')->where(['sp_id' => $id])->select();
        foreach ($lists as $k => $v) {
            $v['link_type'] = $v['type'] == 1 ? '政策支持' : '新闻报道';
            $lists[$k] = $v;
        }
        $this->assign('lists', $lists);
        $this->assign('sp_id', $id);
        $this->display();
    }
    /**
     * desc     服务商连接信息
     */
    public function editsp_link()
    {
        $id         = I('get.id');
        $sp_id      = I('get.sp_id');
        $org_info   = M('sp_link')->where(['id' => $id])->find();
        if (IS_POST) {
            $info = I('post.info');
            if ($id) {
                // 更新
                M('sp_link')->where(['id' => $id])->save($info);
                $this->success("修改成功");
            } else {
                // 新建
                unset($info['id']);
                M('sp_link')->add($info);
                $this->success("添加成功");
            }
        } else {
            $del = I('get.del');
            if ($del) {
                if ($org_info) {
                    M('sp_link')->where(['id' => $id])->delete();
                    $this->success("删除成功");
                } else {
                    $this->error("请求数据有误！");
                }
            }
            if ($org_info) {
                $this->assign('info', $org_info);
            }
            $this->assign('sp_id', $sp_id);
            $this->display();
        }
    }

    /**
     * 服务商列表处理方法,根据不同条件显示不同的列表
     * @param array $where 查询条件
     */
    private function _lists($where = array()){

        if ($company_name = I("post.company_name")) {
            $where['company_name'] = array('like',"%$company_name%");
            $this->assign("company_name", $company_name);
        }
        if ($status = I("post.status", '0', 'intval')) {
            $where['status'] = $status;
            $this->assign("status", $status);
        }

        $this->sp_model->where($where);
        $count  = $this->sp_model->count();
        $page   = $this->page($count, $this->page_size);

        $this->sp_model
            ->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->order("sort desc");

        $sp_lists = $this->sp_model->select();

        $this->assign("page", $page->show('Admin'));
        $this->assign("sp_lists", $sp_lists);
    }
}